#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "los_swtmr.h"
/* Timer count */
UINT32 g_timerCount1 = 0;
UINT32 g_timerCount2 = 0;
/* 任务ID */
UINT32 g_testTaskId01;
void Timer1_Callback() // 回调函数1
{
    UINT32 tick_last1;
    g_timerCount1++;
    tick_last1 = (UINT32)LOS_TickCountGet(); // 获取当前Tick数
    printf("g_timerCount1=%d, tick_last1=%d\n", 
    g_timerCount1, tick_last1);
}
void Timer2_Callback() // 回调函数2
{
    UINT32 tick_last2;
    tick_last2 = (UINT32)LOS_TickCountGet();
    g_timerCount2++;
    printf("g_timerCount2=%d tick_last2=%d\n", g_timerCount2, tick_last2);
}

void Timer_example()
{
    UINT32 id1; // timer id
    UINT32 id2; // timer id
    UINT32 uwTick;
    /*创建单次软件定时器，Tick数为1000，启动到1000Tick
    数时执行回调函数1 */
    LOS_SwtmrCreate(1000, LOS_SWTMR_MODE_ONCE, 
    Timer1_Callback, &id1, 1);
    /*创建周期性软件定时器，每100Tick数执行回调函数2 */
    LOS_SwtmrCreate(100, LOS_SWTMR_MODE_PERIOD, 
    Timer2_Callback, &id2, 1);
    printf("create Timer1 success\n");
    LOS_SwtmrStart(id1); // 启动单次软件定时器
    printf("start Timer1 sucess\n");
    LOS_TaskDelay(200); // 延时200Tick数
    LOS_SwtmrTimeGet(id1, &uwTick); // 获得单次软件定时器剩余Tick数
    printf("uwTick =%d\n", uwTick);
    LOS_SwtmrStop(id1); // 停止软件定时器
    printf("stop Timer1 sucess\n");
    LOS_SwtmrStart(id1);
    LOS_TaskDelay(1000);
    LOS_SwtmrDelete(id1); // 删除软件定时器
    printf("delete Timer1 sucess\n");
    LOS_SwtmrStart(id2); // 启动周期性软件定时器
    printf("start Timer2\n");
    LOS_TaskDelay(1000);
    LOS_SwtmrStop(id2);
    LOS_SwtmrDelete(id2);
}
APP_FEATURE_INIT(Timer_example);